package com.cskaoyan.service;

import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.user.UserExample;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Li Qing
 * @Create: 2020/4/24 15:38
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> queryUsers(String username, String mobile, Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit, true);
        PageHelper.orderBy(sort + " " + order);
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (username != null) criteria.andUsernameLike("%" + username + "%");
        if (mobile != null) criteria.andMobileLike("%" + mobile + "%");
        return userMapper.selectByExample(example);
    }

    /**
     * 查询所有用户数目
     *
     * @return
     */
    @Override
    public Long queryTotalUsers() {
        UserExample example = new UserExample();
        example.createCriteria().andDeletedEqualTo(false);
        return userMapper.countByExample(example);
    }

    /**
     * 查询特定用户密码
     *
     * @param username
     * @return
     */
    @Override
    public List<User> queryUsersByUserName(String username) {
        UserExample example = new UserExample();
        example.createCriteria().andDeletedEqualTo(false).andUsernameEqualTo(username);
        return userMapper.selectByExample(example);
    }
}



